﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Web.Excel;
using S4Store.Domain.CarKpi;
using S4Store.Domain.FinancialKpi;
using S4Store.Domain.NetMarketingKpi;
using S4Store.Domain.SellerKpi;
using S4Store.Domain.TelSaleKpi;
using S4Store.Domain.TelSellerKpi;
using S4Store.Service;
using S4Store.Models.Imports;
namespace S4Store.Controllers
{
    public class ImportController : ReportBaseController
    {

        public TelSaleKpiService TelSaleKpiService { get; set; }
        public  NetMarketingKpiService NetMarketingKpiService { get; set; }
        public  FinancialKpiService FinancialKpiService { get; set; }
        public CarKpiService CarKpiService { get; set; }
        public SellerKpiService SellerKpiService { get; set; }
        public TelSellerKpiService TelSellerKpiService { get; set; }
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }
        public EmployeeService EmployeeService { get; set; }

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            this.InjectReportModel(model);
            model.BarModel.AddView(Models.ReportBarModel.ViewItem.Stores);
            model.BarModel.RemoveView(Models.ReportBarModel.ViewItem.SubmitButton);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "Import";
            model.BarModel.RemoveForm = true;
            return View(model);
        }

        public JsonResult ImportFile(DateTime? importdate, string importfilepath, string importtype)
        {
            if (importdate==null)
            {
                throw new Exception("请填写日期");
            }
            if (String.IsNullOrWhiteSpace(importtype))
            {
                throw new Exception("请填写类型");
            }
            if (String.IsNullOrWhiteSpace(importfilepath))
            {
                throw new Exception("请上传excel");
            }
            if (importtype == "车型KPI")
            {
                return carkpi(importdate, importfilepath);
            }
            if (importtype == "展厅销售KPI")
            {
                return sellerkpi(importdate, importfilepath);
            }
            if (importtype == "电话销售KPI")
            {
                return telsellerkpi(importdate, importfilepath);
            }
            if (importtype == "电销信息管理数据")
            {
                return telsalekpi(importdate, importfilepath);
            }
            if (importtype == "网络营销数据")
            {
                return netMarketingKpi(importdate, importfilepath);
            }
            if (importtype == "财务数据")
            {
                return financialKpi(importdate, importfilepath);
            }
            if (importtype == "售后车型KPI")
            {
                return aftersalecarkpi(importdate, importfilepath);
            }
            if (importtype == "售后用品销售")
            {
                return aftersaledecokpi(importdate, importfilepath);
            }
            if (importtype == "售后服务顾问")
            {
                return aftersalesa(importdate, importfilepath);
            }
            if (importtype == "售后车间主管")
            {
                return aftersalewm(importdate, importfilepath);
            }
            if (importtype == "售后客服专员")
            {
                return aftersalecs(importdate, importfilepath);
            }
            else
            {
                throw new Exception("导入类型错误");
            }
        }

        #region 销售
        #region 车型KPI
        private void checkcarkpi(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                var IsConfirmed = CarKpiService.GetCarKpiDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }
        public JsonResult carkpi(DateTime? importdate, string importfilepath)
        {
            checkcarkpi(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(2);
            sheet.Columns = new List<string>
            {
                "车型","车款","二网订车","本店订车","A类库存","B类库存","C类库存","C类库龄","在途",
                "二网已交","本店已交","H级有望客户","A级有望客户","B级有望客户","H级登记客户","A级登记客户","B级登记客户",
                "投保","置换","按揭","延保","套餐车销量","装潢车装潢台次","装潢车装潢总额"
            };
            sheet.IgnoreColumns = new List<string>
            {
                "车型"
            };
            var items = sheet.GetRowDatas<CarKpiDayData>(new DomainImportHelper<CarKpiDayData>());
            foreach (var item in items)
            {
                item.SetDate(importdate.Value);
                item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                CarKpiService.SubmitCarKpiDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 展厅销售KPI
        private void checksellerkpi(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(SellerKpiService);
                var IsConfirmed = SellerKpiService.GetSellerKpiDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }
        public JsonResult sellerkpi(DateTime? importdate, string importfilepath)
        {
            checksellerkpi(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "销售顾问姓名","车型","首次来店接待批次","首次来店客户登记数","有效A卡建卡数","试驾邀请书填写数","试驾客户未成交数","商谈明细表填写数",
                "超限价格成交数","二手车评估数","置换成交数","按揭成交数","上月遗留客户新增订单","当月客户新增订单数","交车数","新车保险投保数",
                "保险销售额","套餐车销售数量","装潢车销售数","装潢销售额","新车延保数","二次邀约成功数","老客户推荐成交数"
            };
            sheet.IgnoreColumns = new List<string>
            {
                //"车型"
            };
            var items = sheet.GetRowDatas<SellerKpiDayData>(new DomainImportHelper<SellerKpiDayData>());
            foreach (var item in items)
            {
                item.Date=importdate.Value;
                //item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(SellerKpiService);
                SellerKpiService.SubmitSellerKpiDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 电话销售KPI
        private void checktelsellerkpi(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(TelSellerKpiService);
                var IsConfirmed = TelSellerKpiService.GetSellerKpiDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }
        public JsonResult telsellerkpi(DateTime? importdate, string importfilepath)
        {
            checktelsellerkpi(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "销售顾问姓名","车型","首次来店接待批次","首次来店客户登记数","有效A卡建卡数","试驾邀请书填写数","试驾客户未成交数","商谈明细表填写数",
                "超限价格成交数","二手车评估数","置换成交数","按揭成交数","上月遗留客户新增订单","当月客户新增订单数","交车数","新车保险投保数",
                "保险销售额","套餐车销售数量","装潢车销售数","装潢销售额","新车延保数","二次邀约成功数","老客户推荐成交数"
            };
            sheet.IgnoreColumns = new List<string>
            {
                //"车型"
            };
            var items = sheet.GetRowDatas<TelSellerKpiDayData>(new DomainImportHelper<TelSellerKpiDayData>());
            foreach (var item in items)
            {
                item.Date = importdate.Value;
                //item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(TelSellerKpiService);
                TelSellerKpiService.SubmitSellerKpiDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion
        #endregion

        #region 电话销售过程管理
        private void checktelsalekpi(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(TelSaleKpiService);
                var IsConfirmed = TelSaleKpiService.GetTelSaleKpiDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }
        public JsonResult telsalekpi(DateTime? importdate, string importfilepath)
        {
            checktelsalekpi(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "车型","厂方广告获取信息数","自店广告获取信息数","电话登记表登记客户数","12小时电话回复数","线上转来店客户数","来店客户信息录入数","交车数",
                "电话销售目标","网站获取信息数"
            };
            sheet.IgnoreColumns = new List<string>
            {
                "车型"
            };
            var items = sheet.GetRowDatas<CarBrandTelSaleDayData>(new DomainImportHelper<CarBrandTelSaleDayData>());
            foreach (var item in items)
            {
                item.Date=importdate.Value;
                item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(TelSaleKpiService);
                TelSaleKpiService.SubmitTelSaleKpiDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 网络营销数据
        private void checkNetMarketingKpi(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                var IsConfirmed = NetMarketingKpiService.GetDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }
        public JsonResult netMarketingKpi(DateTime? importdate, string importfilepath)
        {
            checkNetMarketingKpi(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "媒体名称","发帖数","点击数","单击退出数","获取信息数","主动来电数","邀约来店数"
            };
            sheet.IgnoreColumns = new List<string>
            {
                "媒体名称"
            };
            var items = sheet.GetRowDatas<NetMarketingKpiDayData>(new DomainImportHelper<NetMarketingKpiDayData>());
            foreach (var item in items)
            {
                item.Date = importdate.Value;
                item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiService);
                NetMarketingKpiService.SubmitDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 财务数据
        private void checkFinancialKpi(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(FinancialKpiService);
                var IsConfirmed = FinancialKpiService.GetFinancialDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }
        public JsonResult financialKpi(DateTime? importdate, string importfilepath)
        {
            checkFinancialKpi(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "车型名称","销售总额","商品车销售额","装潢销售额","保险销售额","贷款服务额","二手车销售额","二手车毛利","延保销售额","售后毛利"
                ,"事故车产值","保养产值","维修产值","备件产值","装潢产值","养护产品产值"
            };
            sheet.IgnoreColumns = new List<string>
            {
                "车型名称"
            };
            var items = sheet.GetRowDatas<FinancialDayData>(new DomainImportHelper<FinancialDayData>());
            foreach (var item in items)
            {
                item.Date = importdate.Value;
                item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(FinancialKpiService);
                FinancialKpiService.SubmitFinancialDayDatas(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 售后
        #region 售后车型

        private void checkaftersalecarkpi(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                var IsConfirmed = AfterSaleCarKPIService.GetAfterSaleCarTypeDayInputs(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }

        public JsonResult aftersalecarkpi(DateTime? importdate, string importfilepath)
        {
            checkaftersalecarkpi(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "车型","事故入厂台次","维修入厂台次","5K保养数","10K保养数","5K保养实际产值","10K保养实际产值","其他常态维修产值",
                "事故车产值","用品销售额","养护产品销售额","首次续保台次","二次续保台次"
            };
            var items = sheet.GetRowDatas<Domain.AfterSaleKpi.AfterSaleCarTypeDayInput>(new DomainImportHelper<Domain.AfterSaleKpi.AfterSaleCarTypeDayInput>());
            foreach (var item in items)
            {
                item.SetDate(importdate.Value);
                item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                AfterSaleCarKPIService.SubmitAfterSaleCarTypeDayDataList(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 用品销售

        private void checkaftersaledecokpi(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                var IsConfirmed = AfterSaleCarKPIService.GetAfterSaleCarDecoratingDayInputs(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }

        public JsonResult aftersaledecokpi(DateTime? importdate, string importfilepath)
        {
            checkaftersaledecokpi(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "车型","交车台次","装潢销售台次","装潢销售总额","养护产品销售台次","养护产品销售总额"
            };
            var items = sheet.GetRowDatas<Domain.AfterSaleKpi.AfterSaleCarDecoratingDayInput>(new DomainImportHelper<Domain.AfterSaleKpi.AfterSaleCarDecoratingDayInput>());
            foreach (var item in items)
            {
                item.AddTime = importdate.Value;
                item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                AfterSaleCarKPIService.SubmitAfterSaleCarDecoratingDayDataList(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, Store.Id, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 服务顾问

        private void checkaftersalesa(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, Domain.EmployeeType.ServiceConsultant).Select(o=>o.Id).ToArray();
                var IsConfirmed = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputs(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, employs).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }

        public JsonResult aftersalesa(DateTime? importdate, string importfilepath)
        {
            checkaftersalesa(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "售后前台接待姓名","A卡转C卡","新增六个月未回厂台次","新增四个月未回厂台次","事故入厂台次","维修入厂台次",
                "事故车产值","维修产值","实施5K保养数","实施10K保养数","电话招徕客户数","电话成功预约回厂数","实现预约回厂数",
                "准时交车客户数","当日交车客户数","累计用品销售额","累计养护产品销售额","首次续保实际完成台次","二次续保实际完成台次","流失客户重新回厂数",
                "满意度回访平均得分"
            };
            var items = sheet.GetRowDatas<Domain.AfterSaleKpi.AfterSaleServiceAdvisorDayInput>(new DomainImportHelper<Domain.AfterSaleKpi.AfterSaleServiceAdvisorDayInput>());
            foreach (var item in items)
            {
                item.AddTime = importdate.Value;
                //item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, Domain.EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();
                AfterSaleCarKPIService.SubmitServiceAdvisorDayInputsList(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, employs, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 车间主管

        private void checkaftersalewm(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, Domain.EmployeeType.WorkshopSupervisor).Select(o => o.Id).ToArray();
                var IsConfirmed = AfterSaleCarKPIService.GetAfterSaleWorkshopManagerDayInputs(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, employs).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }

        public JsonResult aftersalewm(DateTime? importdate, string importfilepath)
        {
            checkaftersalewm(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "车间主管姓名","本月工单总数","本月累计入厂台次","本月累计维修产值","本月已交车总数","技工出勤时间总和","部门定额工时累计总数",
                "功能完好设备工具完好数","无效工单数量","预计时间前交车总数","当日入厂当日交车总数","技工实际作业时间总和","部门实际工时累计总数","交车后回厂返修总数"
            };
            var items = sheet.GetRowDatas<Domain.AfterSaleKpi.AfterSaleWorkshopManagerDayInput>(new DomainImportHelper<Domain.AfterSaleKpi.AfterSaleWorkshopManagerDayInput>());
            foreach (var item in items)
            {
                item.AddTime = importdate.Value;
                //item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, Domain.EmployeeType.WorkshopSupervisor).Select(o => o.Id).ToArray();
                AfterSaleCarKPIService.SubmitWorkshopManagerDayInputsList(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, employs, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion

        #region 客服专员

        private void checkaftersalecs(DateTime? importdate)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, Domain.EmployeeType.AccountSpecialist).Select(o => o.Id).ToArray();
                var IsConfirmed = AfterSaleCarKPIService.GetAfterSaleCustomerServiceDayInputs(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, employs).Any(x => x.IsConfirmed);
                if (IsConfirmed && CurrentUser.Position != Domain.Position.SystemManager)
                {
                    throw new Exception("此日数据已确认，你无权限修改数据");
                }
                t.Commit();
            }
        }

        public JsonResult aftersalecs(DateTime? importdate, string importfilepath)
        {
            checkaftersalecs(importdate);
            ExcelDocument exceldoc = new ExcelDocument(Server.MapPath(importfilepath));
            ExcelSheet sheet = exceldoc.GetSheet(0);
            sheet.SetHeaderRowLine(1);
            sheet.Columns = new List<string>
            {
                "售后前台接待姓名","累计维修后回访数","六月未回厂客户回访数","四月未回厂客户回访数","流失客户重新回厂数"
            };
            var items = sheet.GetRowDatas<Domain.AfterSaleKpi.AfterSaleCustomerServiceDayInput>(new DomainImportHelper<Domain.AfterSaleKpi.AfterSaleCustomerServiceDayInput>());
            foreach (var item in items)
            {
                item.AddTime = importdate.Value;
                //item.StoreId = Store.Id;
                item.IsConfirmed = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, Domain.EmployeeType.AccountSpecialist).Select(o => o.Id).ToArray();
                AfterSaleCarKPIService.SubmitCustomerServiceDayInputsList(importdate.Value.Year, importdate.Value.Month, importdate.Value.Day, employs, items);
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    msg = "导入成功"
                }
            };
        }
        #endregion
        #endregion
    }
}
